package com.shhm.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.shhm.admin.mapper.UserMapper;
import com.shhm.admin.service.UserService;
import com.shhm.common.dto.response.AjaxResultPlus;
import com.shhm.common.dto.request.RequestList;
import com.shhm.common.entity.admin.SysUser;
import com.shhm.admin.util.RequestUtil;
import com.shhm.common.mapper.SysRoleOperatePermsMapper;
import com.shhm.common.utils.MybatisPlusUtil;
import com.shhm.common.utils.ResponseUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author JackZ
 * @version 1.0
 * @description: TODO
 * @date 2025/8/1 下午2:38
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private SysRoleOperatePermsMapper roleOperatePermsMapper;
    @Resource
    private PasswordEncoder passwordEncoder;


    @Override
    public AjaxResultPlus userList() {
        return AjaxResultPlus.custom(200,false)
                .put("userList", userMapper.selectList(null));
    }

    @Override
    public AjaxResultPlus pageList(Integer pageNum, Integer pageSize, HttpServletRequest request) {
        PageDTO<SysUser> userPage = new PageDTO<>(pageNum, pageSize);
        String userOperatePerms = RequestUtil.getUserOperatePerms(request, roleOperatePermsMapper);
//        if (userOperatePerms.equals("NOT_PERMISSION")) return AjaxResultPlus.error(403,"您无权查看数据");
        PageDTO<SysUser> list=userMapper.selectPage(userPage, null);
        return AjaxResultPlus.custom(200, false)
                .put("data",list)
                .put("operateStr",userOperatePerms);
    }


    @Override
    public AjaxResultPlus update(SysUser user) {
        return ResponseUtil.sqlExeIsSuccess(userMapper.updateById(user));
    }

    @Override
    public AjaxResultPlus add(SysUser user) {
        String handlePassword = passwordEncoder.encode(user.getPassword());
        user.setPassword(handlePassword);
        user.setCreateTime(LocalDateTime.now());
        return ResponseUtil.sqlExeIsSuccess(userMapper.insert(user));
    }

    @Override
    public AjaxResultPlus delete(RequestList ids) {
        List<Object> list = ids.getList();
        return ResponseUtil.sqlExeIsSuccess(userMapper.deleteByIds(list));
    }
    @Override
    public AjaxResultPlus pageSearchList(Integer pageNum, Integer pageSize, SysUser sysUser) {
        PageDTO<SysUser> userPage = new PageDTO<>(pageNum, pageSize);
        QueryWrapper<SysUser> qw = MybatisPlusUtil.buildSearchQueryWrapper(sysUser);
        PageDTO<SysUser> list = userMapper.selectPage(userPage, qw);
        return AjaxResultPlus.success(list,false);
    }

}
